import socket,select,time
listenL = []
for i in range(7890,7899):
    add = ('0.0.0.0',i)
    s = socket.socket()
    s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    s.bind(add)
    s.listen(300)
    listenL.append(s)
sockMap = {}
uTime = time.monotonic()
while True:
    rl = list(sockMap.keys())+listenL
    rl ,wl,el = select.select(rl,[],[],0.5)    
    tt = time.monotonic()
    for one in rl:
        if one in listenL and len(sockMap)<300:
            try:
                a,b = one.accept()
                sockMap[a] = tt  
            except Exception as e:
                print (e)

        elif one in sockMap:
            try:
                r = one.recv(10000)
                if not r:
                    one.close()
                    del sockMap[one]
                    continue
                sockMap[one] = tt
                one.sendall(r)  
            except Exception as e:
                one.close()
                del sockMap[one]
                print (e)                
    if uTime+1<tt:
        uTime = tt        
        for k in list(sockMap.keys()):
            if sockMap[k]+1<tt:
                del sockMap[k]
                k.close()
        print (uTime,len(sockMap))

    